package com.cooldatasoft.rest.controller;

import java.io.IOException;

import lombok.extern.slf4j.Slf4j;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@Controller
public class SampleController {

	@ResponseBody
	@RequestMapping(value = "/{fromCurrency}/{toCurrency}", method = RequestMethod.GET)
	public String getExchangeRate(@PathVariable String fromCurrency, @PathVariable String toCurrency) {
		long requestStart = System.nanoTime();

		ObjectMapper mapper = new ObjectMapper();
		try {
			long executionTime = System.nanoTime() - requestStart;
			log.info("Conversion from {} to {} , Execution time : {}ns", new Object[] { fromCurrency, toCurrency, executionTime });
			return mapper.writeValueAsString("value");
		} catch (JsonGenerationException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "Connection Error!";
	}

}
